package org.zju.demo.mina;

import org.springframework.beans.factory.annotation.Autowired;
import org.zju.demo.model.CarStatus;
import org.zju.demo.service.impl.CarStatusService;
import org.zju.demo.service.intf.CarStatusIntf;

import java.util.Date;

/**
 * Created by ZJDX on 2015/9/17.
 */
public class CarHandler extends AbstractHandler {

    private static final int COMMAND_STATE = 0x01;
    private static final int COMMAND_TEMPERATURE = 0x02;
    private static final int COMMAND_HUMIDITY = 0x03;

    private static final int TURN_RIGHT = 1;
    private static final int TURN_LEFT = 2;
    private static final int TURN_FORWARD = 3;
    private static final int STOP = 4;

    private final CarStatusIntf carStatusService = new CarStatusService();

    public CarHandler(byte[] buffer) {
        super(buffer);
    }

    @Override
    public String handle() {
        int id = getId();
        int data = getData();
        switch (getCommandType()) {
            case COMMAND_STATE:
                if (data > 4) {
                    // invalid
                    return null;
                } else {
                    //
                    CarStatus carStatus = new CarStatus();
                    carStatus.setTime(new Date());
                    carStatus.setCarId(id);
                    carStatus.setStatus(data);
                    carStatusService.add(carStatus);
                }
                break;
            case COMMAND_HUMIDITY:
                break;
            case COMMAND_TEMPERATURE:
                break;
            default:
        }
        return null;
    }
}

